#include "core.h"
#include "xRoomMgr.h"


CRoom::CRoom(U32 room_type, U64 guid)
: xObjBase(static_cast<CGuidMgr::_eEntityType>(room_type), guid)
{
	m_nLevelID = 0;
}

void CRoom::LoadLevel()
{

}
////////////////////////////////////////////////////////////////////////////////////////////////////////////

IMP_SINGLETON_CLASS(CRoomMgr)

CRoomMgr::CRoomMgr()
{

}

CRoomMgr::~CRoomMgr()
{
	Clear();
}

void CRoomMgr::Clear()
{
	for(LevelRoomList::iterator it = m_allRooms.begin(); it != m_allRooms.end(); )
	{
		RoomList* pRL = it->second;
		for (RoomList::iterator i = pRL->begin(); i != pRL->end(); )
		{
			CRoom* pR = i->second;
			delete pR;
			i = pRL->erase(i);
		}
		delete pRL;
		it = m_allRooms.erase(it);
	}
}

RoomList* CRoomMgr::GetRoomsByLevelID(U32 nLevelID)
{
	RoomList* pRet = NULL;
	LevelRoomList::iterator it = m_allRooms.find(nLevelID);
	if (it == m_allRooms.end())
	{
		pRet = new RoomList;
		m_allRooms.insert(LevelRoomList::value_type(nLevelID, pRet));
	}
	else
	{
		pRet = it->second;
	}

	return pRet;
}

CRoom* CRoomMgr::CreateRoom(U32 nLevelID, BOOL bStatic)
{
	CGuidMgr::_eEntityType et = bStatic ? CGuidMgr::ENTITY_STATIC_ROOM : CGuidMgr::ENTITY_DYNAMIC_ROOM;
	U64 uGuid = CGuidMgr::getInstance()->GenGUID(et, nLevelID);
	CRoom* pR = new CRoom(et, uGuid);
	if (pR)
	{
		RoomList* rl = GetRoomsByLevelID(nLevelID);
		if (rl)
		{
			rl->insert(RoomList::value_type(uGuid, pR));
		}

		return pR;
	}
	return NULL;
}